<?php

function encodingUTF8($string){
    $fs = "utf-8";
    $ts = "euc-kr";
    if($string === mb_convert_encoding ( mb_convert_encoding ( $string, $fs, $ts ), $ts, $fs )){
        $string = iconv($ts, $fs, $string);
    }

    return $string;
}

/**
 * Xml read
 */
function xmlRead($_file)
{
    $data = file_get_contents($_file);

    return $data;
}

/**
 * XML Parsing
 */
function xmlParsing($_xml)
{
    App::import('Xml');

    try
    {
        $parsed_xml = Xml::toArray(Xml::build($_xml));
    }
    catch (XmlException $e)
    {
        return false;
    }

    return $parsed_xml;
}

function nowDateTime()
{
    $now_time = date('Y-m-d H:i:s', time());
    return $now_time;
}

function blockStatus($_status)
{
    if($_status == 0) $status = 'X';
    else $status = 'O';

    return $status;
}
/**
 * gift item options
 */
function giftOption()
{
    $data = array(
        'empty' => __('Empty'),
        'gold' => __(LANG_GOLD),
        'gem' => __(LANG_GEM),
        'item' => __(LANG_ITEM)
    );

    return $data;
}

/**
 * gift item options
 */
function cashOption()
{
    $data = array(
        9000 => 10,
        18000 => 21,
        45000 => 55,
        90000 => 115
    );

    return $data;
}

function cashAndGem($_cash)
{
    $data = array(
        9000 => array('cash' => 9000, 'gem' => 10, 'product_id' => 'a2xgames_tod_inapp_product1', 'gem_use_type' => 24),
        18000 => array('cash' => 18000, 'gem' => 21, 'product_id' => 'a2xgames_tod_inapp_product2', 'gem_use_type' => 25),
        45000 => array('cash' => 45000, 'gem' => 55, 'product_id' => 'a2xgames_tod_inapp_product3', 'gem_use_type' => 26),
        90000 => array('cash' => 90000, 'gem' => 115, 'product_id' => 'a2xgames_tod_inapp_product4', 'gem_use_type' => 27),
    );

    foreach($data as $key => $value)
    {
        if($key == $_cash)
        {
            $cash_gem = $value;
            break;
        }
    }

    return $cash_gem;
}

function arrayToJson($array)
{
    if(!is_array($array))
    {
        return false;
    }

    $associative = count(array_diff(array_keys($array), array_keys(array_keys($array))));
    if($associative)
    {
        $construct = array();
        foreach($array as $key => $value)
        {
            if(is_numeric($key))
            {
                $key = "key_$key";
            }

            $key = '"'.addslashes($key).'"';

            if(is_array($value))
            {
                $value = arrayToJson($value);
            }
            else if(!is_numeric($value) || is_string($value))
            {
                $value = '"'.addslashes($value).'"';
            }

            $construct[] = "$key:$value";
        }

        $result = "{" . implode(",", $construct) . "}";

    }
    else
    {
        $construct = array();

        foreach($array as $value)
        {
            if(is_array($value))
            {
                $value = arrayToJson($value);
            }
            else if(!is_numeric($value) || is_string($value))
            {
                $value = '"'.addslashes($value).'"';
            }

            $construct[] = $value;
        }

        $result = "[" . implode( ",", $construct) . "]";
    }

    return $result;
}

/**
 * number check and array
 */
function numberCheck($_data)
{
    if(is_array($_data))
    {
        foreach($_data as $data)
        {
            if(is_array($data))
            {
                foreach($data as $value)
                {
                    if(!preg_match("/^[0-9]+$/", $value))
                    {
                        return false;
                    }
                }
            }
            else
            {
                if(!preg_match("/^[0-9]+$/", $data))
                {
                    return false;
                }
            }
        }
    }
    else
    {
        if(!preg_match("/^[0-9]+$/", $_data))
        {
            return false;
        }
    }

    return true;
}

/**
 * null check
 */
function nullCheck($_data)
{
    if(is_array($_data))
    {
        foreach($_data as $value)
        {
            if($value == null)
            {
                return true;
            }
        }
    }
    else
    {
        if($_data == null)
        {
            return true;
        }
    }

    return false;
}

/**
 * 날짜 형식 체크
 */
function dateCheck($_date)
{
    if(isset($_date))
    {
        $parse_arg = date_parse($_date);

        if(!empty($parse_arg['error_count']))
        {
            return false;
        }
    }

    return true;
}

/**
 * array empty delete
 */
function emptyArrayUnset($_data)
{
    foreach($_data as $value)
    {
        if(empty($value))
        {
            unset($value);
        }
        else
        {
            $data[] = $value;
        }
    }

    return $data;
}

/**
 *
 */
function dateDiff($_date_start, $_date_end)
{
    $date_start = date_parse($_date_start);
    $date_end = date_parse($_date_end);

    $datetime_start = mktime($date_start['hour'],$date_start['minute'],$date_start['second'],$date_start['month'],$date_start['day'],$date_start['year']);
    $datetime_end = mktime($date_end['hour'],$date_end['minute'],$date_end['second'],$date_end['month'],$date_end['day'],$date_end['year']);

    $datetime_now = mktime(date('H'), date('i'), date('s'), date('m'), date('d'), date('Y'));

    if($datetime_start > $datetime_now || $datetime_end < $datetime_now)
    {
        return true;
    }

    return false;
}

function encryptMd5Base64($plain_text, $password="password", $iv_len = 16)
{
    $plain_text .= "\x13";
    $n = strlen($plain_text);
    if ($n % 16) $plain_text .= str_repeat("\0", 16 - ($n % 16));
    $i = 0;
    $enc_text = '';

    while ($iv_len-- >0)
    {
        $enc_text .= chr(mt_rand() & 0xff);
    }

    $iv = substr($password ^ $enc_text, 0, 512);
    while($i <$n)
    {
        $block = substr($plain_text, $i, 16) ^ pack('H*', md5($iv));
        $enc_text .= $block;
        $iv = substr($block . $iv, 0, 512) ^ $password;
        $i += 16;
    }
    return base64_encode($enc_text);
}

function decryptMd5Base64($enc_text, $password="password", $iv_len = 16)
{
    $enc_text = base64_decode($enc_text);
    $n = strlen($enc_text);
    $i = $iv_len;
    $plain_text = '';
    $iv = substr($password ^ substr($enc_text, 0, $iv_len), 0, 512);
    while($i <$n)
    {
        $block = substr($enc_text, $i, 16);
        $plain_text .= $block ^ pack('H*', md5($iv));
        $iv = substr($block . $iv, 0, 512) ^ $password;
        $i += 16;
    }
    return preg_replace('/\x13\x00*$/', '', $plain_text);
}

function staticsYearOption()
{
    for($i=2013; $i<=2016; $i++)
    {
        $data[$i] = $i . '년';
    }

    return $data;
}

function staticsMonthOption()
{
    $data = array(
        '1' => 'January(1)',
        '2' => 'February(2)',
        '3' => 'March(3)',
        '4' => 'April(4)',
        '5' => 'May(5)',
        '6' => 'June(6)',
        '7' => 'July(7)',
        '8' => 'August(8)',
        '9' => 'September(9)',
        '10' => 'October(10)',
        '11' => 'November(11)',
        '12' => 'December(12)',
    );

    return $data;
}